<?php

namespace Yolo\Log;

/**
 * FileLog
 * @author lushaoming<lushaoming6@gmail.com>
 * @date 2024/1/31
 */
class FileLog extends AbstractLog
{
    protected function writeLog(EnumLogLevel $level, string|array $log): void
    {
        \Hyperf\Coroutine\go(function () use ($level, $log) {

            if (is_string($log)) {
                $text = [
                    'action' => 'log',
                    'message' => $log,
                    'data' => []
                ];
            } else {
                $text = $log;
            }

            $text['timestamp'] = time();

            $logPath = BASE_PATH . '/storage/logs';
            if (!is_dir($logPath)) {
                @mkdir($logPath, 0644, true);
            }

            file_put_contents($logPath . '/' . date('Ymd') . '.log',
                json_encode($text) . PHP_EOL,
                FILE_APPEND
            );
        });
    }
}
